from OWRpy import *
import os.path, redREnviron
import redRi18n
_ = redRi18n.get_(package = 'base')
import signals
import redRGUI

class panelutils(OWRpy): 
    globalSettingsList = ['commit']
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)

        # R code for generating the coldiss plot
        self.R('source(\''+unicode(os.path.join(redREnviron.directoryNames['libraryDir'], 'ecology', 'widgets', 'panelutils.R')).replace('\\', '//')+'\')')
        
        # Python variables
        self.data = {}
        self.RFunctionParam_object = ''
        
        #Inputs
        self.inputs.addInput('id0', _('R Variable Object'), signals.base.RDataFrame, self.processobject) # creer un signal pour dissimilarity object
		
        #GUI
        self.RFunctionParam_main = redRGUI.base.lineEdit(self.controlArea, label = "Main title: ", text = '')
        self.RFunctionParam_win = redRGUI.base.checkBox(self.controlArea,  label = "Options", 
        buttons = [('win','Plot in external window.')], orientation='horizontal')
        self.plotArea = redRGUI.plotting.redRPlot(self.controlArea, label = "Plot", displayLabel = False)
        self.plotArea.resizeCheck.uncheckAll()
        self.plotArea.topArea.hide()

        #Commit
        self.commit = redRGUI.base.commitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignLeft, 
        callback = self.commitFunction, processOnInput=True)
        
    def processobject(self, data):
        if not self.require_librarys(["graphics"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        if data:
            self.RFunctionParam_object=data.getData()
            self.data = data
            if self.commit.processOnInput():
                self.commitFunction()
        else:
            self.RFunctionParam_object=''
    def commitFunction(self):
        
        if unicode(self.RFunctionParam_object) == '':
            self.status.setText('No data.')
            return

        self.plotArea.plotMultiple(query = unicode(self.RFunctionParam_object)+', lower.panel=panel.smooth, upper.panel=panel.cor, diag.panel=panel.hist, main="'+unicode(self.RFunctionParam_main.text())+'"', function = 'pairs')
            
        if 'win' in self.RFunctionParam_win.getCheckedIds(): 
            self.R('pairs('+unicode(self.RFunctionParam_object)+', lower.panel=panel.smooth, upper.panel=panel.cor, diag.panel=panel.hist, main="'+unicode(self.RFunctionParam_main.text())+'")')
        
        
